package cloud.maque.admin.impl;

import cloud.maque.admin.domain.entity.MqXzqChange;
import cloud.maque.admin.mapper.MqXzqChangeMapper;
import cloud.maque.admin.service.MqXzqChangeService;
import cloud.maque.framework.common.request.PaginateDataResquest;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 4color
 * @description mq_xzq_change
 * @date 2023-03-17
 */
@Service
public class MqXzqChangeServiceImpl extends ServiceImpl<MqXzqChangeMapper, MqXzqChange> implements MqXzqChangeService {


    @Override
    public PageInfo<List<MqXzqChange>> listByPage(PaginateDataResquest<MqXzqChange> queryRequest) {
        if (queryRequest.getPageIndex() > 0 && queryRequest.getPageSize() > 0) {
            PageHelper.startPage(queryRequest.getPageIndex(), queryRequest.getPageSize());
        }

        QueryWrapper<MqXzqChange> queryWrapper = new QueryWrapper<>();
        if (queryRequest.getData() != null) {
            if (!StrUtil.isEmptyIfStr(queryRequest.getData().getMemo())) {
                queryWrapper.lambda().or().like(MqXzqChange::getAfterXzqName, queryRequest.getData().getMemo());
                queryWrapper.lambda().or().like(MqXzqChange::getBeforeXzqName, queryRequest.getData().getMemo());
                queryWrapper.lambda().or().like(MqXzqChange::getAfterXzqCode, queryRequest.getData().getMemo());
                queryWrapper.lambda().or().like(MqXzqChange::getBeforeXzqCode, queryRequest.getData().getMemo());

            }

            if (!StrUtil.isEmptyIfStr(queryRequest.getData().getMcaTime())) {
                queryWrapper.lambda().or(temp -> temp.eq(MqXzqChange::getMcaTime, queryRequest.getData().getMcaTime()));
            }
        }


        List<MqXzqChange> list = this.baseMapper.selectList(queryWrapper);

        PageInfo pageInfo = new PageInfo<>(list);

        return pageInfo;
    }

    /**
     * 只列出跟行政区有关的数据
     *
     * @param xzq
     * @return
     */
    @Override
    public List<MqXzqChange> listByXzq(String xzq) {
        QueryWrapper<MqXzqChange> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().or().eq(MqXzqChange::getBeforeXzqCode, xzq);
        queryWrapper.lambda().or().eq(MqXzqChange::getAfterXzqCode, xzq);
        queryWrapper.lambda().orderByDesc(MqXzqChange::getMcaTime);
        List<MqXzqChange> list = this.baseMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<String> listMcmtime() {

        QueryWrapper<MqXzqChange> queryWrapper = new QueryWrapper<>();

        queryWrapper.lambda().select(MqXzqChange::getMcaTime);
        queryWrapper.lambda().groupBy(MqXzqChange::getMcaTime);
        queryWrapper.lambda().orderByDesc(MqXzqChange::getMcaTime);

        List<MqXzqChange> list = this.baseMapper.selectList(queryWrapper);

        List<String> listR = new ArrayList<>();

        if (list != null && list.size() > 0) {
            listR = list.stream().map(s -> {
                String t = "";
                if (s != null && s.getMcaTime() != null) {
                    t = DateUtil.format(s.getMcaTime(), "yyyy-MM-dd");
                }
                return t;
            }).collect(Collectors.toList());
        }

        return listR;
    }

    @Override
    public MqXzqChange Save(MqXzqChange xzq) {
        if (StrUtil.isEmptyIfStr(xzq.getChangeId())) {
            xzq.setChangeId(UUID.fastUUID().toString());
        }
        this.saveOrUpdate(xzq);
        return xzq;
    }

    @Override
    public Boolean Delete(String xzqId) {
        return this.removeById(xzqId);
    }
}
